﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace RN.ERP.Web
{
    public class DESHelper
    {

        /// <summary>
        /// des
        /// </summary>
        /// <param name="plainText"></param>
        /// <param name="key">必须8为字符</param>
        /// <param name="iv">必须8为字符</param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static string Encrypt(string plainText, string key, string iv, Encoding encode)
        {
            // Create a new DES key.
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = encode.GetBytes(plainText);
            des.Key = encode.GetBytes(key);
            des.IV = encode.GetBytes(iv);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }


        /// <summary>
        /// Encrypt the string
        /// </summary>
        /// <param name="plainText"></param>
        /// <param name="key">必须8为字符</param>
        /// <param name="iv">必须8为字符</param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static byte[] Encrypt2(string plainText, string key, string iv, Encoding encode)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = encode.GetBytes(plainText);
            des.Key = encode.GetBytes(key);
            des.IV = encode.GetBytes(iv);
            // Create a memory stream.
            MemoryStream ms = new MemoryStream();

            // Create a CryptoStream using the memory stream and the 
            // CSP DES key.  
            CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

            // Create a StreamWriter to write a string
            // to the stream.
            StreamWriter sw = new StreamWriter(encStream);

            // Close the StreamWriter and CryptoStream.
            sw.Close();
            encStream.Close();

            // Get an array of bytes that represents
            // the memory stream.
            byte[] buffer = ms.ToArray();

            // Close the memory stream.
            ms.Close();

            // Return the encrypted byte array.
            return buffer;
        }


        /// <summary>
        /// Decrypt the byte array.
        /// </summary>
        /// <param name="stringToDecrypt"></param>
        /// <param name="key">必须8为字符</param>
        /// <param name="iv">必须8为字符</param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static string Decrypt(byte[] stringToDecrypt, string key, string iv, Encoding encode)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Key = encode.GetBytes(key);
            des.IV = encode.GetBytes(iv);
            // Create a memory stream to the passed buffer.
            MemoryStream ms = new MemoryStream(stringToDecrypt);

            // Create a CryptoStream using the memory stream and the 
            // CSP DES key. 
            CryptoStream encStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read);

            // Create a StreamReader for reading the stream.
            StreamReader sr = new StreamReader(encStream);

            // Read the stream as a string.
            string val = sr.ReadLine();

            // Close the streams.
            sr.Close();
            encStream.Close();
            ms.Close();

            return val;
        }

        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="stringToDecrypt">欲解密的错</param>
        /// <param name="key">必须8为字符</param>
        /// <param name="iv">必须8为字符</param>
        /// <param name="encode">编码方式</param>
        /// <returns></returns>
        public static string Decrypt(string stringToDecrypt, string key, string iv, Encoding encode)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Key = encode.GetBytes(key);
            des.IV = encode.GetBytes(iv);
            byte[] inputByteArray = new byte[stringToDecrypt.Length / 2];
            for (int x = 0; x < stringToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(stringToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            //return System.Text.Encoding.Default.GetString(ms.ToArray());
            return encode.GetString(ms.ToArray());

        }
    }
}